class User < ActiveRecord::Base
  attr_accessible :password, :address, :city, :country, :email, :first_name, :index_phone, :last_name, :password_confirmation, :phone, :user_type_id, :zip_code
  attr_accessor :password, :password_confirmation
  validates :password,  :presence => true
  before_save :encrypt_password

  belongs_to :user_type, foreign_key: "user_type_id"
  has_many :hotels
    
  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

    def self.authenticate(email, password)
      user = find_by_email(email)
      if user and user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end
end
